package com.songlanyun.modules.usercard.controller;

import cn.hutool.core.map.MapUtil;
import com.songlanyun.common.bank.BankCardInfo;
import com.songlanyun.common.bank.BankConfig;
import com.songlanyun.common.utils.R;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.usercard.entity.UserCardEntity;
import com.songlanyun.modules.usercard.mobel.CheckBankDto;
import com.songlanyun.modules.usercard.mobel.UserCardConfirmDto;
import com.songlanyun.modules.usercard.mobel.UserCardDto;
import com.songlanyun.modules.usercard.mobel.UserCardRemoveDto;
import com.songlanyun.modules.usercard.service.UserCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;



/**
 * 用户绑定的银行卡（为了快捷支付）
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2023-03-22 11:16:38
 */
@RestController
@RequestMapping("api/usercard")
public class UserCardApiController extends AbstractApiController {
    @Autowired
    private UserCardService userCardService;




    /**
     * 查询用户的银行卡
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("usercard:usercard:info")
    public R info(@PathVariable("id") Long id){
		UserCardEntity userCard = userCardService.loadByUserIdAndId(getUserId(true),id,false);

        return R.ok().put("userCard", userCard);
    }

    /**
     * 查询用户的银行卡
     */
    @GetMapping("/list")
    //@RequiresPermissions("usercard:usercard:info")
    public R list(@RequestParam Map<String, Object> params){
        Integer valid = MapUtil.getInt(params, "valid", -1);
        List<UserCardEntity> userCards = userCardService.loadList(getUserId(true),valid);

        return R.ok().put("list", userCards);
    }


    /**
     * 创建快捷绑卡
     */
    @PostMapping("/save")
    //@RequiresPermissions("usercard:usercard:save")
    public R createCardConfirm(@RequestBody @Validated UserCardDto dto){
        UserCardEntity userCard =userCardService.add(getUserId(true),dto);
        return R.ok().put("data",userCard);
    }

    /**
     * 创建快捷绑卡确认
     */
    @PostMapping("/createCardConfirm")
    //@RequiresPermissions("usercard:usercard:save")
    public R create(@RequestBody @Validated UserCardConfirmDto dto){
        userCardService.createCardConfirm(getUserId(true),dto);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/remove")
    //@RequiresPermissions("usercard:usercard:delete")
    public R remove(@RequestBody UserCardRemoveDto dto){
        dto.setUserId(getUserId(true));
		userCardService.delete(dto);
        return R.ok();
    }

    /**
     * 查银行卡
     */
    @PostMapping("/check")
    //@RequiresPermissions("usercard:usercard:delete")
    public R check(@RequestBody CheckBankDto dto){
        //getUserId(true);
        BankCardInfo info = BankConfig.loadBankNameByCardId(dto.getCardId());
        BankConfig.validate(info);
        return R.ok().put("data",info);
    }

    /**
     * 查银行卡logo
     */
    @GetMapping("/getBankLogo")
    //@RequiresPermissions("usercard:usercard:delete")
    public R check(@RequestParam(value = "bank_mark",required = true) String bankMark){
        //getUserId(true);
        String logoStream = BankConfig.loadBankLogo(bankMark);
        return R.ok().put("data",logoStream);
    }

}
